package Modelo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.Table;


@Entity
@Table(name = "r")
@NamedQueries({@NamedQuery(name = "R.findAll", query = "SELECT r FROM R r"), @NamedQuery(name = "R.findById", query = "SELECT r FROM R r WHERE r.id = :id"), @NamedQuery(name = "R.findByFecha", query = "SELECT r FROM R r WHERE r.fecha = :fecha")})
public class R implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;

    private Long idEncuesta;
    private String idProyecto;
    private Long idPersona;

    @Basic(optional = false)
    @Column(name = "fecha")
    private String fecha;
    @OneToMany (cascade=CascadeType.ALL)
    private List<Registro> registros;

    public R() {
        registros=new ArrayList<Registro>();
    }

    public R(Long id) {
        this.id = id;
    }

    public R(Long id, String fecha) {
        this.id = id;
        this.fecha = fecha;

    }

    public R(String fecha) {
        this.fecha = fecha;
        registros=new ArrayList<Registro>();
    }

    private R(String fecha, Long idpersona, Long idencuesta, String idproyecto) {
       this.fecha = fecha;
       registros=new ArrayList<Registro>();
       this.idEncuesta=idencuesta;
       this.idPersona=idpersona;
       this.idProyecto=idproyecto;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFecha() {
        return fecha;
    }

    public void setFecha(String fecha) {
        this.fecha = fecha;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof R)) {
            return false;
        }
        R other = (R) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "pack.R[id=" + id + "]";
    }
    public Long agregarR(String fecha) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        R reg = new R(fecha);
        em.getTransaction().begin();
        em.persist(reg);
        em.getTransaction().commit();
        Query q = em.createQuery("select Max(c.id) from R c");
        return (Long) q.getSingleResult();
    }
     public Long agregarR(String fecha,Long idpersona, Long idencuesta, String idproyecto) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        R reg = new R(fecha,idpersona,idencuesta,idproyecto);
        em.getTransaction().begin();
        em.persist(reg);
        em.getTransaction().commit();
        Query q = em.createQuery("select Max(c.id) from R c");
        return (Long) q.getSingleResult();
    }

    public void asignarRegistro(Long idReg, Long idPre, Long idEtiq, Long idRes) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        R reg = em.find(R.class, idReg);
        //reg.addR(r);
        reg.addRegistro(idPre,idEtiq,idRes);
        em.getTransaction().begin();
        em.persist(reg);
        em.getTransaction().commit();
        em.close();
        emf.close();
    }



        private void addRegistro(Long idPre, Long idEtiq, Long idRes) {
        this.registros.add(new Registro(idPre,idEtiq,idRes));
    }

    
}
